//
//  THJLoginVC.m
//  THJXHW
//
//  Created by 河南德艺 on 17/4/12.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJLoginVC.h"
#import "THJRegistVC.h"
#import "NSString+String.h"
#import "THJForgetPswVC.h"
#import "JPNavigationController.h"
#import "THJEditUserInfoVC.h"
#import "THJQuickLoginView.h"
#import "ZBNetworking.h"

@interface THJLoginVC ()<THJQuickLoginViewDelegate,UITextFieldDelegate>
{
    NSString * isFirstLogin;
}
@end

@implementation THJLoginVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [self setStateBarColor:[UIColor clearColor]];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    [self setUpNav];
    self.secretTF.secureTextEntry = YES;
    self.acountTF.keyboardType = UIKeyboardTypeNumberPad;
    self.acountTF.delegate = self;
    isFirstLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"firstlogin"];
}
- (IBAction)loginBtnClicked:(id)sender {
    BOOL isPhoneNum = [NSString isMobileNumber:self.acountTF.text];
    if (self.acountTF.text.length==0) {
        [self HUDShowMsg:@"请输入账号" AfterDelayDimiss:1];
        return;
    }
    if (!isPhoneNum){
        [self HUDShowMsg:@"请输入正确手机号" AfterDelayDimiss:1];
        return;
    }
    if (self.secretTF.text.length==0) {
        [self HUDShowMsg:@"请输入登录密码" AfterDelayDimiss:1];
        return;
    }
    [MBProgressHUD showMessage:@"正在登录" toView:self.view];
    NSDictionary * dic = @{
                           @"mobile":self.acountTF.text,
                           @"password":self.secretTF.text
                           };
    NSString * urlStr = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=login&r=login"];
    __weak typeof(self) weakself = self;
    [THJNetWork postLoginFromUrl:urlStr parameters:dic callback:^(NSDictionary* receiveData, NSError *error) {
        NSLog(@"登录--%@",receiveData);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
         [self HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
        if([receiveData[@"status"] isEqual:@"200"]){
            [[NSUserDefaults standardUserDefaults] setObject:receiveData[@"data"] forKey:@"token"];
            [[NSUserDefaults standardUserDefaults] setObject:self.acountTF.text forKey:@"MOBILE"];
            [weakself performSelector:@selector(pop) withObject:nil afterDelay:1];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:nil];
            [weakself getUserInfo];
        }
    }];
}
-(void)pop{
    [self dismissViewControllerAnimated:NO completion:nil];
}
#pragma mark ---忘记密码---
- (IBAction)forgetSecretBtnClicked:(id)sender {
    UINavigationController * nvc = [[UINavigationController alloc] initWithRootViewController:[THJForgetPswVC new]];
    [self presentViewController:nvc animated:YES completion:nil];
}
#pragma mark ---注册----
- (IBAction)registerAcountClicked:(id)sender {
    UINavigationController * nvc =  [[UINavigationController alloc] initWithRootViewController:[THJRegistVC new]];
    [self presentViewController:nvc animated:YES completion:nil];
}
/**
 验证码快速登录
 */
- (IBAction)quickLoginAction:(id)sender {
//    [self pop];
//    [[LBTabBarController sharedTabBarC] presentViewController: animated:NO completion:nil];
    THJQuickLoginView * view = [THJQuickLoginView instancView];
    view.frame = CGRectMake(0, 80, ScreenWidth, 350);
    view.delegate = self;
    [self.view addSubview:view];
//    [self.navigationController pushViewController:[THJQuickLoginVC new] animated:YES];
}
-(void)loginSuccesed{
    [self pop];
}
- (IBAction)backBtnAction:(id)sender{
    [self pop];
}
-(void)getUserInfo{
    NSString * urlstr = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"dingjia.php?f=Dan&r=Dan&m=hj"];
    NSString * token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString=urlstr;
        request.apiType=ZBRequestTypeRefresh;
        [request setValue:token forHeaderField:@"token"];
    } success:^(id responseObject,apiType type){
        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"合金宝状态-%@",receiveData);
        if([receiveData[@"status"] isEqual:@"200"]){
                [THJTools setHjbUser:YES];
                NSLog(@"HJBUSER_YES");
        }else{
                [THJTools setHjbUser:NO];
                NSLog(@"HJBUSER_NO");
        }
    } failed:^(NSError *error){
        
    }];
}
@end
